package com.fattyCorps.game.service.player;

import com.fattyCorps.game.exception.game.CoinLackErrorException;
import com.fattyCorps.game.exception.game.MoneyLackErrorException;
import com.fattyCorps.game.obj.cache.PlayerInfo;

public enum EProp {
    // 铜币
    Money {
        @Override
        int getVal(PlayerInfo player) {
            return player.getPlayerData().getMoney();
        }

        @Override
        void setVal(PlayerInfo player, Integer val) {
            player.getPlayerData().setMoney(val);
        }

        @Override
        void onLackErr(PlayerInfo player) {
            throw new MoneyLackErrorException();
        }
    },

    // 元宝
    Coin {
        @Override
        int getVal(PlayerInfo player) {
            return player.getPlayerData().getCoin();
        }

        @Override
        void setVal(PlayerInfo player, Integer val) {
            player.getPlayerData().setCoin(val);
        }

        @Override
        void onLackErr(PlayerInfo player) {
            throw new CoinLackErrorException();
        }
    };

    abstract int getVal(PlayerInfo player);
    abstract void setVal(PlayerInfo player, Integer val);
    abstract void onLackErr(PlayerInfo player);

    public boolean gain(PlayerInfo playerInfo, int num) {
        Integer curVal = getVal(playerInfo);
        setVal(playerInfo, curVal + num);
        return true;
    }

    public boolean consume(PlayerInfo playerInfo, int num) {
        Integer curVal = getVal(playerInfo);

        if (num < 0) {
            // 扣钱
            if (curVal < -num) {
                // 不足
                return false;
            }
        } else if (num == 0) {
            return true;
        }

        Integer newCoin = curVal + num;
        setVal(playerInfo, newCoin);

        return true;
    }

    public void consumeThrow(PlayerInfo playerInfo, int num) {
        if (!consume(playerInfo, num)) {
            onLackErr(playerInfo);
        }
    }
}
